﻿






$.MGJ.NodeConfig = [];

var  NodeConfig = $.MGJ.NodeConfig;

(function () {
    var _this = $.MGJ.NodeConfig;

    $.extend($.MGJ.NodeConfig, {

        //批次列表  点击溯源节点配置【在新增编辑模板库里面 这个函数不需要】
        Dialog_NodeConfigObj: null,
        Dialog_NodeConfig: function (batchId) {
            this.Dialog_NodeConfigObj = $.L.dialog({
                url: ACTION_URL("NodeConfig", "OriginsNode") + "?batchId=" + batchId,
                title: "节点配置",
                width: 1100,
                height: 'auto',
                buttons: {
                    "button_save": {
                        type: 'primary',
                        text: '保存',
                        click: 'MODALMISS'
                    },
                    "button_cancel": {
                        type: 'default',
                        text: '取消',
                        click: 'MODALMISS'
                    }
                }
            });

        },

        //根据模板ID获取节点列表
        Ajax_GetTempletNodeList: function (templetId) {
            $.L.ajaxHandle({
                url: ACTION_URL("_GetTempletNodeList", "OriginsNode"),
                data: { templetId: templetId },
                success: function (data) {
                    $("#NodeData").L_bindingDataList(data);
                }
            })
        },

        //编辑节点数据
        Ajax_EditNode: function (obj) {
            var nodeId = $(obj).attr("NodeId");

            $.L.ajaxHandle({
                url: ACTION_URL("GetNodeModel", "OriginsNode") + "?nodeId=" + nodeId,
                success: function (data) {
                    _this.L_bindingNode(data);
                    for (var i = 0; i < $(".AddContent").length; i++) {
                        for (var n = 0; n < data.Display.length; n++) {
                            if (i == data.Display[n]) {
                                $(".AddContent").eq(i).addClass("green-500")
                            }
                        }
                    }

                }
            })
        },

        Ajax_NodeSave: function (templetId) {

            var chk_value = "";
            $('input[name="DisplayChe"]:checked').each(function () {
                chk_value += $(this).val() + ",";
            });
            $("#Display").val(chk_value.substring(0, chk_value.length - 1));

            $.L.ajaxHandle({
                url: ACTION_URL("_NodeSave", "OriginsNode"),
                data: $("#Node_form").serialize(),
                success: function (ht) {

                    if (ht.result > 0) {
                        _this.Ajax_GetTempletNodeList(templetId);
                        _this.NodeInfoIniti();
                        $("#SourceNodeRight").hide();
                    }
                }
            });

        },
        //点击 添加节点 
        Ajax_AddNode: function () {

            _this.NodeInfoIniti();
            $("#SourceNodeRight").show();
        },
        //删除节点
        Ajax_DeleteNode: function (obj) {

            var nodeId = $(obj).attr("NodeId");
            var templetId = $("#FlowID").val();
            var result = confirm('是否删除！');
            if (result) {
                $.L.ajaxHandle({
                    url: ACTION_URL("NodeDelete", "OriginsNode") + "?nodeId=" + nodeId,
                    success: function (result) {
                        if (result > 0) {
                            _this.Ajax_GetTempletNodeList(templetId);
                        }
                    }
                });
            }
        },
        //点击编辑 绑定数据
        L_bindingNode: function (data) {
            _this.NodeInfoIniti();
            $("#SourceNodeRight").show();
            if (data.Permission == 0) {
                $("#Modifier").hide();
            } else {
                $("#Modifier").show();
            }

            $("#id").val(data.Id);
            $("#NodeName").val(data.NodeName);
            $("#Category").find("option[value='" + data.Category + "']").prop("selected", true);
            $("input[type='radio'][name='EnterTimes'][value='" + data.EnterTimes + "']").prop("checked", true);
            $("input[type='radio'][name='IsH5'][value='" + data.IsH5 + "']").prop("checked", true);
            $("input[type='radio'][name='Permission'][value='" + data.Permission + "']").prop("checked", true);
            $("#UserSelect").val(data.PermissionPerson);

            if (data.Display != "" || data.Display != null) {
                if (data.Display.length > 1) {
                    $(data.Display.split(',')).each(function (i, e) {
                        $("input[type='checkbox'][name='DisplayChe'][value='" + e + "']").prop("checked", "checked");
                    });
                }
                else {
                    $("input[type='checkbox'][name='DisplayChe'][value='" + data.Display + "']").prop("checked", "checked");
                }
            }
        },
        //节点信息初始化
        NodeInfoIniti: function () {
            $("#id").val(0);
            $("#NodeName").val("");
            $("#SourceNodeRight").find("input").prop("checked", false);
            $("#SourceNodeRight").find("select").find("option").eq(0).prop("selected", true);
            $(".AddContent").removeClass("green-500");
        },
        GetUserListByEid: function () {

            $.L.ajaxHandle({
                url: ACTION_URL("GetSysUserListByEID", "Manager"),
                success: function (result) {
                    if (result.length > 0) {
                        _this.BindingUserSelect(result);
                    }
                }
            });

        },

        BindingUserSelect: function (list) {
            var html = "";
            for (var i = 0; i < list.length; i++) {
                html += "<option value='" + list[i].ID + "'>" + list[i].RealName + "</option>"
            }
            $("#UserSelect").html(html);
        },

        //从模板添加
        Dialog_FromTempletObj: null,
        Dialog_FromTemplet: function (templetId) {
            this.Dialog_FromTempletObj = $.L.dialog({
                url: ACTION_URL("FromTemplet", "OriginsNode"),
                title: "从模板添加",
                width: 1100,
                height: 'auto',
                buttons: {
                    "button_save": {
                        type: 'primary',
                        text: '保存',
                        click: function () {
                            _this.Ajax_setTemplete(templetId);
                        }
                    },
                    "button_cancel": {
                        type: 'primary',
                        text: '返回',
                        click: 'MODALMISS'
                    }
                }
            });
        },

        Ajax_setTemplete: function (templetId) {
            var sysTempletId = $("input[name='tdRadio']:checked").val();
            if (sysTempletId == undefined) {
                $.L.msgWarning('请选择模版');
                return;
            }
          
            $.L.ajaxHandle({
                url: ACTION_URL("SetTemplet", "OriginsNode"),
                data: { sysTempletId: sysTempletId, templetId: templetId },
                success: function (result) {
                    if (result > 0) {
                        if (confirm("操作成功，是否返回上一页面？")) {

                            _this.Dialog_FromTempletObj.close();

                            _this.Ajax_GetTempletNodeList(templetId);
                        }
                    }
                }
            });
        },

        getCategoryName: function (val) {

            if (val == 0) {
                return "原料节点";
            } else if (val == 1) {
                return "关键节点"
            } else {
                return "其他"
            }
        },


        getEnterTimes: function (val) {

            if (val == 0) {
                return "单次";
            } else if (val == 1) {
                return "多次"
            }
        }



    })

})(jQuery);



$(document).ready(function () {

    $.MGJ.NodeConfig.GetUserListByEid();

});


$('#Node_form').bootstrapValidator({
    fields: {
        NodeName: {
            validators: {
                notEmpty: {
                    message: '节点名称不能为空'
                },
            }
        },
        Category: {
            validators: {
                notEmpty: {
                    message: '节点类型不能为空'
                },
            }
        },
        EnterTimes: {
            validators: {
                notEmpty: {
                    message: '录入限制不能为空'
                },
            }
        },
        IsH5: {
            validators: {
                notEmpty: {
                    message: 'H5控制不能为空'
                },
            }
        },
        Permission: {
            validators: {
                notEmpty: {
                    message: '修改权限不能为空'
                },
            }
        },
    }
});







//点击保存节点
function NodeSave(value) {
    var bootstrapValidator = $('#Node_form').data('bootstrapValidator').validate();//触发全部验证
    if (bootstrapValidator.isValid()) {
        if ($('input[name="DisplayChe"]:checked').val() != undefined) {
            NodeConfig.Ajax_NodeSave(value)
        } else {
            $.L.msgError("必须添加至少一种内容！");
        }
    }
    else {
        $.L.msgError("请检查数据格式！");
    };
}






//删除
function NodeMiniRemove(_this) {
    var result = confirm('是否删除！');
    if (result) {
        $(_this).parents(".SourceNodeLeftChild").remove()
    }
}




$('input:radio[name="Permission"]').change(function (e) {
    var val = $(this).val();
    if (val == 1) {
        $("#Modifier").show();
    } else {
        $("#Modifier").hide();
    }
});

function Modifier() {
    if ($("input[name='PermissionPerson'][value=1]").is(":checked")) {
        $("#Modifier").show()
    } else {
        $("#Modifier").hide()
    }
}


//点击添加内容
$(".AddContentInp").on("click", function () {
    $(this).parents(".AddContent").toggleClass("green-500")
})



//完成编辑
function CompleteEdit() {


    var a = 0;
    $("#SourceNodeRight").find("input[type='radio']").each(function () {
        if ($(this).context.checked) {
            a++
        }
    })

    var SelevtIndex = $("#SourceNodeRight").find("select").val()

    if (!$("#SourceNodeRight").find("input").val() == "") {
        if (SelevtIndex != "0") {
            if (a >= $("#SourceNodeRight").find("input[type='radio']").length / 2) {

                var $AfterHtml = [
                    '<div class="SourceNodeLeftChild ">',
                    '<div class="border-buttom padding-10 ">',
                        '<span class="LeftChild1F padding-left-10 padding-right-10">1</span>',
                        '<input type="text" style="border:0" />',
                        '<span class="fr">',
                            '<i class="glyphicon glyphicon-edit" onclick="EditNodeToRight(this)"></i>&nbsp;',
                            '<i class="glyphicon glyphicon-remove" onclick="NodeMiniRemove(this)"></i>',
                        '</span>',
                    '</div>',
                    '<ul class="margin-left-10 margin-right-10">',
                        '<li class="border-buttom padding-10">',
                            '<span class="padding-right-20">节点名称</span>',
                            '<span style="color:#999999">' + $("#NodeName").val() + '+</span>',
                        '</li>',
                        '<li class="padding-10">',
                            '<span class="padding-right-20">节点类型</span>',
                            '<span>',
                                '<a class="click_a color-999999">原料节点</a>&nbsp;',
                                '<a class="click_a color-999999">录入限制</a>&nbsp;',
                                '<a class="click_a color-999999">多次</a>',
                            '</span>',
                        '</li>',
                    '</ul>',
                '</div>',
                ].join("")
                $("#SourceNodeLeft_").append($AfterHtml)
                $(".SourceNodeLeftChild:last").find(".click_a").eq(parseInt(SelevtIndex) - 1).addClass("active")
                $("#SourceNodeRight").hide()
            } else {
                $.L.msgError("有单选项未选择")
            }
        } else {
            $.L.msgError("节点类型未选择")
        }
    } else {
        $.L.msgError("节点配置未填写")
    }
}